package io.github.talelin.latticy.common.util;

import com.alibaba.excel.util.DateUtils;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BarcodeQRCode;
import com.itextpdf.text.pdf.PdfWriter;
import io.github.talelin.latticy.common.pojo.QRCodeEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * 生成可带文字的二维码工具类
 */
@Slf4j
public class QRCodeToPDF {

    public void createPDF(QRCodeEntity qrCodeEntity) throws DocumentException {
        FileOutputStream out = qrCodeEntity.getFileOutputStream();
        Document document = new Document();
        // 虽然没被使用，但是如果不写的话传到前端的文件会无法加载
        PdfWriter pdfWriter = PdfWriter.getInstance(document, out);
        document.open();
        int size = qrCodeEntity.getCodelist().size();
        for (int i = 0; i < size; i++) {
            document.add(new Paragraph(qrCodeEntity.getCodelist().get(i).getCodeTitle()));
            BarcodeQRCode qrcode = new BarcodeQRCode(qrCodeEntity.getCodelist().get(i).getCodeScan(),
                    qrCodeEntity.getCodeWidth(),
                    qrCodeEntity.getCodeHeight(),
                    null);
            Image qrcodeImage = qrcode.getImage();
            qrcodeImage.setAbsolutePosition(qrCodeEntity.getAbsoluteX(), qrCodeEntity.getAbsoluteY());
            document.add(qrcodeImage);
        }
        document.close();
    }


    public static void printPdf() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();
        try {
            Document document = new Document();
            ServletOutputStream out = response.getOutputStream();
            // ⽂件输出流指向PDF⽂件
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            PdfWriter writer = PdfWriter.getInstance(document, bao);
            // 查询SKU信息
            List<Map<String, Object>> orderList = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            map.put("name", "银杆 & 3x5ft 210D美国国旗");
            map.put("sku", "sku-01");
            orderList.add(map);
            if (CollectionUtils.isEmpty(orderList)) {
                log.error("⽣成PDF失败 --> ");
                throw new Exception("⽣成PDF失败,未查询到数据!");
            }
            // 编辑PDF
            PdfClient.createPdf(writer, document, orderList,"sku-01");
            // 设置页⾯编码格式
            response.setContentType("application/pdf");
            response.setContentLength(bao.size());
            response.setHeader("Content-Disposition", "inline;filename=" + new String(("pdfname"+ DateUtils.format(new Date(), DateUtils.DATE_FORMAT_14)).getBytes(), StandardCharsets.ISO_8859_1) + ".pdf");
            // 实现页⾯预览PDF
            bao.writeTo(out);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出PDF出错 --> e={}", e.getMessage());
        }
    }
}